<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="推送标题" prop="messageTitle">
        <el-input
          v-model="queryParams.messageTitle"
          placeholder="请输入推送标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['weiriya:message:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weiriya:message:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weiriya:message:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="messageList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="55" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送类型" width="180" align="center">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.messageType === 1
                ? 'success'
                : scope.row.messageType === 2
                ? 'warning'
                : 'danger'
            "
            effect="plain"
          >
            {{
              scope.row.messageType === 1
                ? "提醒消息"
                : scope.row.messageType === 2
                ? "公告消息"
                : "消费消息"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="推送标题" align="center" prop="messageTitle" />
      <el-table-column label="推送内容" header-align="center">
        <template slot-scope="scope">
          <div v-html="scope.row.messageContent" />
        </template>
      </el-table-column>
      <el-table-column label="已读状态" align="center" width="180">
        <template slot-scope="scope">
          <el-popover placement="top" @show="handleView(scope.row)" trigger="hover">
            <div>{{ readInfo }}</div>
            <el-button slot="reference" type="text">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weiriya:message:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="`${form.id ? '修改' : '添加'}消息`"
      :visible.sync="open"
      width="50%"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="推送标题">
          <el-input
            v-model="form.messageTitle"
            placeholder="请输入推送标题"
            maxlength="30"
            clearable
          />
        </el-form-item>
        <el-form-item label="推送内容">
          <editor v-model="form.messageContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="消息类型">
          <el-radio-group v-model="form.messageType" @change="changeRange">
            <el-radio :label="1">提醒消息</el-radio>
            <el-radio :label="2">公告消息</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推送范围">
          <el-radio-group v-model="form.messageRange" @change="changeRange">
            <el-radio :label="2">全部</el-radio>
            <el-radio :label="1">按人员</el-radio>
            <el-radio :label="3">按区域</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择人员" v-if="form.messageRange == 1">
          <el-select
            v-model="form.memberId"
            multiple
            filterable
            clearable
            style="width: 100%"
            placeholder="账号搜索"
          >
            <el-option
              v-for="item in personList"
              :key="item.id"
              :label="item.nickName"
              :value="item.id"
            >
              <span>{{ item.nickName }}</span>
              <span v-if="item.userName" style="color: #ccc">
                ({{ item.realName || item.userName }})
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择区域" v-if="form.messageRange == 3">
          <el-select
            v-model="form.areaId"
            multiple
            filterable
            clearable
            style="width: 100%"
            placeholder="点位搜索"
          >
            <el-option
              v-for="item in areaList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              <span>{{ item.name }}</span>
              <span v-if="item.address" style="color: #ccc"> ({{ item.address }})</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage,
  delMessage,
  addMessage,
  getMessageState,
} from "@/api/weiriya/message";
import { listArea } from "@/api/weiriya/area";
import { listMember } from "@/api/weiriya/member";
export default {
  name: "Message",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      messageList: [], // 平台消息推送管理表格数据
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageTitle: null,
      },
      form: {}, // 表单参数
      areaList: [], // 区域列表
      personList: [], // 人员列表
      rules: {}, // 表单校验
      readInfo: "", // 已读情况
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询停车点位列表
    getAreaList() {
      let data = {
        pageNum: 1,
        pageSize: 1000,
      };
      listArea(data).then((res) => {
        this.areaList = res.rows;
      });
    },
    // 查询人员列表
    getPersonList() {
      let data = {
        pageNum: 1,
        pageSize: 1000,
      };
      listMember(data).then((res) => {
        this.personList = res.rows;
      });
    },
    // 查询平台消息推送管理列表
    getList() {
      this.loading = true;
      listMessage(this.queryParams)
        .then((res) => {
          this.messageList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消
    cancel() {
      this.open = false;
      this.reset();
    },
    // 推送范围改变
    changeRange(val) {
      if (val === 1) {
        this.form.areaId = null;
        this.getPersonList();
      } else if (val === 2) {
        this.form.areaId = null;
        this.form.memberId = null;
      } else if (val === 3) {
        this.form.memberId = null;
        this.getAreaList();
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        messageRange: 2, // 推送范围 1指定用户，2全部用户，3指定区域
        messageTitle: null, // 推送标题
        messageContent: null, // 推送内容
        messageType: 2, // 推送类型 1提醒消息，2公告消息
        areaId: null, // 推送区域ID
        memberId: null, // 推送人员ID
      };
      this.resetForm("form");
    },
    // 获取阅读状态
    handleView(row) {
      this.readInfo = "";
      getMessageState(row.id).then((res) => {
        if (res.code == 200) {
          this.readInfo = res.msg;
        }
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 新增
    handleAdd() {
      this.reset();
      this.open = true;
    },
    // 提交
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.form);
          data.memberId = data.memberId ? data.memberId.join(",") : null;
          data.areaId = data.areaId ? data.areaId.join(",") : null;
          addMessage(data).then((res) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除平台消息推送管理编号为"' + ids + '"的数据项？')
        .then(() => {
          delMessage(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      this.download(
        "weiriya/message/export",
        {
          ...this.queryParams,
        },
        `message_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep p img {
  max-width: 100px;
  max-height: 100px;
  object-fit: cover;
}
</style>
